package com.example.ams.repository;

import com.example.ams.entity.TravelRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 出差记录数据访问层
 * Travel Record Repository
 */
@Repository
public interface TravelRecordRepository extends JpaRepository<TravelRecord, Long> {

    /**
     * 根据员工ID查询出差记录
     */
    List<TravelRecord> findByEmployeeIdOrderByStartTimeDesc(Long employeeId);

    /**
     * 根据员工ID和时间范围查询
     */
    @Query("SELECT tr FROM TravelRecord tr WHERE tr.employeeId = :employeeId " +
           "AND tr.startTime >= :startTime AND tr.startTime <= :endTime " +
           "ORDER BY tr.startTime DESC")
    List<TravelRecord> findByEmployeeIdAndTimeRange(
            @Param("employeeId") Long employeeId,
            @Param("startTime") Date startTime,
            @Param("endTime") Date endTime);

    /**
     * 根据时间范围查询所有记录
     */
    @Query("SELECT tr FROM TravelRecord tr WHERE tr.startTime >= :startTime " +
           "AND tr.startTime <= :endTime ORDER BY tr.startTime DESC")
    List<TravelRecord> findByTimeRange(
            @Param("startTime") Date startTime,
            @Param("endTime") Date endTime);

    /**
     * 根据状态查询
     */
    List<TravelRecord> findByStatusOrderByStartTimeDesc(TravelRecord.Status status);
}

